// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/deliverorder/api/WoDeliverPlanApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/deliverorder/model/QueryWoDeliverPlanListReq'
import OneClass from '@api/deliverorder/model/QueryWoDeliverPlanDetailReq'
import AddClass from '@api/deliverorder/model/InsertWoDeliverPlanReq'
import EditClass from '@api/deliverorder/model/UpdateWoDeliverPlanReq'
import DelClass from '@api/deliverorder/model/DeleteWoDeliverPlanReq'
import BaseClass from '@api/deliverorder/model/WoDeliverPlanDto'
import PeriodClass from '@api/deliverorder/model/WoDeliverPlanCycleDto'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'woDeliverPlanDto']
const PERIOD_LIST = [PeriodClass, 'woDeliverPlanCycleDto', 'planCycleDto']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM, BASE)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryWoDeliverPlanList', DTO)
    return [resp.comQuery.totalCount, resp.woDeliverPlanDto || []]
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryWoDeliverPlanDetail', DTO)
    const { woDeliverPlanDto, woDeliverPlanCycleDto } = resp
    return { ...woDeliverPlanDto, planCycleDto: woDeliverPlanCycleDto }
  },
  transOne(obj) {
    obj.planCycleDto.forEach((item, i) => {
      item.index = i + 1
      item.cycleStartTime = `${item.cycleStartHour}:${item.cycleStartMinute}`
      item.cycleEndTime = `${item.cycleEndHour}:${item.cycleEndMinute}`
    })
    return obj
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE, PERIOD_LIST)
    await CORE.grpc(TIP, CLIENT, 'insertWoDeliverPlan', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE, PERIOD_LIST)
    await CORE.grpc(TIP, CLIENT, 'updateWoDeliverPlan', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ids: id })
    await CORE.grpc(TIP, CLIENT, 'deleteWoDeliverPlan', DTO)
    return CORE.grpcOK(TIP)
  },
}
